package org.cnsoft.player.service;

import java.util.List;

import javax.annotation.Resource;

import org.cnsoft.framework.beans.MyBeanFactoryHelper;
import org.cnsoft.framework.beans.user.LoginerBean;
import org.cnsoft.framework.beans.user.UserBean;
import org.cnsoft.framework.support.MyServiceSupport;
import org.cnsoft.framework.utils.EmptyHelper;
import org.cnsoft.framework.weixin.bean.WxCallBackParamBean;
import org.cnsoft.player.ISPLayerRegistService;
import org.cnsoft.player.ISPlayerCheckService;
import org.cnsoft.weixin.player.WeixinPlayerInfoLoadService;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import com.cnsoft.persistent.player.U101111PlayerPassport.U101111PlayerPassportDBO;
import com.cnsoft.persistent.player.U101111PlayerPassport.U101111PlayerPassportDao;
import com.cnsoft.persistent.player.U101112PlayerWeixin.U101112PlayerWeixinDBO;
import com.cnsoft.persistent.player.U101112PlayerWeixin.U101112PlayerWeixinDao;

/**
 * 用户存在性检查
 * 
 * @author ZmSoft
 * @version 0.1.0 2018/3/1
 * @since 0.1.0 2018/3/1
 * @see ApiManagerLoginController
 */
@Service("ISPlayerCheckService") // 替换系统登录接口
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class PlayerCheckService extends MyServiceSupport implements ISPlayerCheckService {

	ISPLayerRegistService myPLayerRegistService;

	public ISPLayerRegistService getMyPLayerRegistService() {
		if (EmptyHelper.isEmpty(myPLayerRegistService))
			myPLayerRegistService = MyBeanFactoryHelper.getBean(ISPLayerRegistService.class);
		return myPLayerRegistService;
	}

	@Resource
	private U101112PlayerWeixinDao daoU101112PlayerWeixin;
	/**
	 * 维修用户信息获取
	 */
	@Resource
	private WeixinPlayerInfoLoadService serviceWeixinPlayerInfoLoad;
	// U101111_player_passport 会员账户密码信息 account_type_code=3

	// 查看微信用户数据
	//// 基于微信openid
	public UserBean checkUser(WxCallBackParamBean wxUserBean, boolean autoRegist) throws Exception {
		UserBean curUser = new UserBean();
		U101112PlayerWeixinDBO weixinUser = new U101112PlayerWeixinDBO();
		weixinUser.setWxOpenId(wxUserBean.getOpenid());
		weixinUser = daoU101112PlayerWeixin.doRead(weixinUser);

		if (EmptyHelper.isEmpty(weixinUser)) {
			// 新用户
			curUser.setRoolType("2");
			if(autoRegist) {
				weixinUser = (U101112PlayerWeixinDBO) getMyPLayerRegistService().doRegist(wxUserBean).getData();
			}else {
				return null;
			}
		} else {
			curUser.setRoolType("1");
		}

		wxUserBean.setRoolType(curUser.getRoolType());// 用户类型（2新注册用户1注册用户0游客）

		// 异步 更新用户全部信息
		wxUserBean.setClientTimestamp(weixinUser.getUpdateTime());// 设定最后更新日期
		serviceWeixinPlayerInfoLoad.doLoad(wxUserBean);// 获取用户微信资料

		curUser.setUserName(weixinUser.getNickName());
		curUser.setLoginSource("10");
		curUser.setLoginType("2");
		curUser.setUserId(weixinUser.getId());
		curUser.setOpenId(weixinUser.getWxOpenId());

		return curUser;
	}

	@Resource
	private U101111PlayerPassportDao daoU101111PlayerPassport;

	// 查看基本用户数据
	// 基于用户信息（手机，账户，ID）
	public UserBean checkUser(LoginerBean loginer, boolean autoRegist) throws Exception {

		UserBean curUser = new UserBean();
		// 检索个人信息
		U101111PlayerPassportDBO param = new U101111PlayerPassportDBO();
		// 登录方式-10账户20手机号30微信31QQ40支付宝50微博60极光99中台
		param.setAccountTypeCode(loginer.getLoginType());
		param.setUserAccount(loginer.getAccount());
		List<U101111PlayerPassportDBO> list = daoU101111PlayerPassport.doSelectData(param);

		if (EmptyHelper.isEmpty(list) || (list.size() > 1)) {
			return null;
		} else {
			param = list.get(0);
		}
		curUser.setUuid(loginer.getUuid().replaceAll("-", ""));
		curUser.setLoginSource("10");
		curUser.setLoginType("2");
		curUser.setRoolType("1");
		curUser.setUserId(param.getId());
		curUser.setAccount(loginer.getAccount());
		return curUser;
	}

}
